<?php
/*!
Application_Model_ComponentDetail
REQUIRES db.php

*/

require("db.php");

class Application_Model_ComponentDetail
{

	public function get_details($id)
	{
		//! Queries $id.\n
		//! Queries $id for ports.\n
		//! Returns associative array EQUALS $id.\n
		
		$qstring = "SELECT * FROM component c, device d, make m, building b, contact co, snmp s WHERE c.component_id = \"" . $id . "\" AND c.device_id = d.device_id AND c.building_id = b.building_id AND co.contact_id = c.contact_id AND s.profile_name = c.profile_name ORDER BY c.location";
		$result = mysql_query($qstring);
		$compiledResults = array();
		$row = mysql_fetch_assoc($result);
		$compiledResults['info'] = $row;
		$qstring = "SELECT p.port, p.port_usage_comments, c.ip_address, c.component_id FROM portusage p, component c WHERE p.from_device=\"".$id."\" AND c.component_id = p.to_device ";
		$result = mysql_query($qstring);
		$compiledResults['portUsage']=array();
		$compiledResults['portUsage']['to']=array();
		$compiledResults['portUsage']['from']=array();
		$compiledResults['portUsage']['services']=array();
		while($toRow = mysql_fetch_assoc($result)){
			array_push($compiledResults['portUsage']['to'],$toRow);
		}
		$qstring="SELECT p.destination_port, p.port_usage_comments, c.ip_address, c.component_id FROM portusage p, component c WHERE p.to_device=\"".$id."\" AND c.component_id = p.from_device ";
		$result = mysql_query($qstring);
		while($fromRow = mysql_fetch_assoc($result)){
			array_push($compiledResults['portUsage']['from'],$fromRow);
		}

		$qstring="SELECT * FROM portusage WHERE component_id=\"".$id."\"";
		$result = mysql_query($qstring);
		while($serviceRow = mysql_fetch_assoc($result)){
			array_push($compiledResults['portUsage']['services'],$serviceRow);
		}
		return $compiledResults;
	}

}

